'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.
/**
 * @fileoverview 获取攻击方的属性数据
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var soldierAttr = require('../../libs/war/soldierAttr');
var surviveConf = require('../../config/surviveConf');
var constantUtil = require('../../util/constantUtil');
var dataApi = require('../../libs/dataApi');

/**
 * 获取来袭数据
 * @param  {[type]} hero_type    [description]
 * @param  {[type]} hero_lv      [description]
 * @return {[type]}              [description]
 */
exports.getData = function (wave_num) {
    var data = _getDataNoSoldier(wave_num);
    var soldier_type_list = [];
    _.each(data.wave_soldier_list, function (soldier_item) {
        soldier_type_list.push({
            soldier_type: soldier_item.soldier_type,
            soldier_star: soldier_item.soldier_star
        });
    });
    soldier_type_list = _.uniqWith(soldier_type_list, _.isEqual);

    data.soldier_list = _getSoldierList(soldier_type_list);

    return data;
};

exports.getDataNoSoldier = function (wave_num) {
    return _getDataNoSoldier(wave_num);
};

var _getDataNoSoldier = function (wave_num) {
    var survive_conf = surviveConf.getWaveConf(wave_num);
    return {
        wave_num: wave_num,
        wave_soldier_list: _getWaveSoldierList(survive_conf.soldier_info),
        soldier_span: constantUtil.value('rebel_soldier_span') / 100
    };
};

/**
 * 获取士兵列表
 * @param  {[type]} soldier_info [description]
 * @return {[type]}           [description]
 */
var _getWaveSoldierList = function (soldier_info) {
    var soldier_all_conf = soldier_info.split(',');
    var soldier_list = [];
    _.each(soldier_all_conf, function (soldier_conf) {
        var soldier_factor = _getSoldierFactor(soldier_conf);
        soldier_list.push({
            soldier_type: soldier_factor.soldier_type,
            soldier_star: soldier_factor.soldier_star,
            factor: soldier_factor.factor
        });
    });
    return soldier_list;
};

var _getSoldierFactor = function (soldier_conf) {
    var soldier_type_factor = soldier_conf.split(':');
    return {
        soldier_type: +soldier_type_factor[0],
        factor: +soldier_type_factor[1],
        soldier_star: +soldier_type_factor[2]
    };
};

var _getSoldierList = function (soldier_type_list) {
    var soldier_list = [];
    _.each(soldier_type_list, function (soldier_item) {
        var survive_soldier_conf = dataApi.function_soldier_base.findById(soldier_item.soldier_type);
        var soldier_attr = soldierAttr.getAttr(survive_soldier_conf.id, 1, soldier_item.soldier_star);
        soldier_attr.attr.min_atk = survive_soldier_conf.atk;
        soldier_attr.attr.max_atk = survive_soldier_conf.atk;
        soldier_attr.attr.crit = survive_soldier_conf.crit;
        soldier_attr.attr.dodge = survive_soldier_conf.dodge;
        soldier_attr.attr.hit = survive_soldier_conf.hit;
        soldier_attr.attr.hp = survive_soldier_conf.hp;
        soldier_attr.attr.uncrit = survive_soldier_conf.uncrit;
        soldier_list.push(soldier_attr);
    });
    return soldier_list;
};
